#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re

import allure
from playwright.sync_api import Page

from models.base_page import BasePage


class LoadbalancerPage(BasePage):
    def __init__(self, page: Page):
        super().__init__(page)
        self.route = "loadbalancers"

    def check_lb_page_user_guide_element_to_be_visible(self):
        """检查使用指南元素是否可见"""
        with allure.step("检查LBPage:使用指南元素是否可见"):
            self.expect.to_be_visible(self.page.locator(".css-rru6sl"))  # 使用指南栏目
            self.expect.to_be_visible(self.page.get_by_text("使用指南"))
            self.expect.to_be_visible(self.page.get_by_text("收起"))
            self.expect.to_be_visible(self.page.locator(".css-rru6sl > span > .qicon-svg > use"))
            self.expect.to_contain_text(self.page.locator("#micro-iaas-root"),
                                        "负载均衡（Load Balancer，简称LB）提供安全快捷的流量分发服务，"
                                        "来自多个公网地址的访问流量经由 LB 可以自动分配到多台云服务器上，"
                                        "并支持自动检测并隔离故障云服务器，提供业务系统的服务能力和可用性。"
                                        "负载均衡支持千万级别并发访问请求，可轻松应对大流量访问，满足业务需求。")

    def check_lb_page_table_element_to_be_visible(self):
        """检查工具栏及table是否可见"""
        with allure.step("检查LBPage:工具栏及table是否可见"):
            self.expect.to_be_visible(self.page.locator(".css-1axy5jn"))
            self.expect.to_be_visible(self.page.get_by_role("button", name="创建负载均衡器"))
            self.expect.to_be_visible(self.page.get_by_role("button", name="更多操作"))
            self.expect.to_be_visible(self.page.get_by_placeholder("请输入名称/实例ID/公网IP/内网IP"))
            self.expect.to_be_visible(self.page.locator("div").filter(has_text=re.compile(r"^标签$")).nth(2))
            self.expect.to_be_visible(self.page.locator("#micro-iaas-root").get_by_role("button").nth(2))
            self.expect.to_be_visible(self.page.locator("#micro-iaas-root").get_by_role("button").nth(3))